'use strict';

/* Controllers */
// signin controller
app.controller('ForgotpwdFormController', ['$scope', '$http', '$state', 'ajax', '$location', function ($scope, $http, $state, ajax, $location) {
	$scope.code = null;
	var phoneNum;
	$scope.authError = null;

	if ($.cookie("captcha")) {
		var count = $.cookie("captcha");
		var btn = $('#send');
		btn.html(count + '秒后可重新获取').attr('disabled', true).css('cursor', 'not-allowed');
		var resend = setInterval(function () {
			count--;
			if (count > 0) {
				btn.html(count + '秒后可重新获取').attr('disabled', true).css('cursor', 'not-allowed');
				$.cookie("captcha", count, { path: '/', expires: (1 / 86400) * count });
			} else {
				clearInterval(resend);
				btn.html("发送").removeClass('disabled').removeAttr('disabled style');
			}
		}, 1000);
	}

	$scope.send = function () {
		if ($scope.phone == null) {
			return;
		}
		var reg = /^1[3|4|5|7|8][0-9]\d{8}$/;
		if (!reg.test($scope.phone)) {
			return;
		}

		$scope.isCollapsed = false;
		phoneNum = $scope.phone;

		var btn = $('#send');
		var count = 60;
		var resend = setInterval(function () {
			count--;
			if (count > 0) {
				btn.html(count + "秒后可重新获取");
				$.cookie("captcha", count, { path: '/', expires: (1 / 86400) * count });
			} else {
				clearInterval(resend);
				btn.html("获取验证码").removeAttr('disabled style');
			}
		}, 1000);
		btn.attr('disabled', true).css('cursor', 'not-allowed');
		
		
		//var promise = ajax.call(new Req.GET({
			//resource: 'public/' + $scope.phone + '/verify'
		//}));
		var url = "http://127.0.0.1:8080/ecar/r/public/"+$scope.phone+"/verify";
    	//将参数传递的方式改成form
    	
		$http.get(
				url
    	).then(function (result) {

			if (result.data.code == 200) {//获取验证码
				$scope.code = result.result;
			} else {
				$scope.authError = '服务器错误，请联系管理员！';
			}

		}, function (data) { });
	};

	$scope.validateUserPwd = function () {
		var pwd = $scope.passward;
		var patten = /^[A-Za-z0-9\!\@\#\$\%\^\&\*\(\)\_\+\`\~\-\=]{6,22}$/;
		if (!patten.test(pwd)) {
			$scope.authError = '密码格式为6到22位字母、数字和字符~!@#$%^&*()_+`-=！';
			return false;
		} else {
			return true;
		}
	};

	$scope.modifyPwd = function () {
		if ($scope.code == null || $scope.code != $scope.phoneCode) {
			//$scope.$apply($scope.authError = '验证码错误！');
			$scope.authError = '验证码错误！';
			return;
		}
		if (!$scope.validateUserPwd()) return;
		/*inserter.resource = 'signup/'+phoneNum+'/'+$scope.passward+'/modifyPwd';
		inserter.ajax();*/
		var pwd = $scope.passward;
		//var inserter = new Req.POST({ resource: 'public/' + phoneNum + '/' + $scope.code + '/password'+'?pwd='+pwd });
		var data = {
    			params:{
    				fields:{
    						username: $scope.user.username,
    						password: $scope.user.password,
    						browser: $window.navigator.userAgent
    						}
    				}
    			};
    	//post请求的地址
    	var url = "http://127.0.0.1:8080/ecar/r/public/" + phoneNum + "/" + $scope.code + "/password"+"?pwd="+pwd;
    	
    	
		//var promise = ajax.call(inserter);
    	$http.post(
    			url,data
    	).then(function (result) {
			if (result.code == 200) {
				$(".step1").hide();
				$(".step2").fadeIn(300);
				setTimeout("$('.step2 span').empty().append('2')", 1000);
				setTimeout("$('.step2 span').empty().append('1')", 2000);
				setTimeout("$('.step2 span').empty().append('0')", 3000);
				setTimeout("window.location.replace('#/access/signin/')", 4000);
			} else if (result.code == 400) {
				$scope.authError = '手机号错误！';
			} else {
				$scope.authError = '服务器错误，请联系管理员！';
			}
		}, function (data) {
		})

		$scope.code = null;
	}

}])
	;